package com.jnks.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jnks.constant.R;
import com.jnks.exception.ServiceException;
import com.jnks.mapper.CompanyWhitelistDroneTMapper;
import com.jnks.pojo.domain.vo.WhiteDroneReqVo;
import com.jnks.pojo.entity.CompanyWhitelistDroneTDO;
import com.jnks.service.CompanyWhitelistDroneTService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 重点单位（反恐）白名单无人机基本信息表 服务实现类
 * </p>
 *
 * @author Ryo
 * @since 2024-04-12
 */
@Service
public class CompanyWhitelistDroneTServiceImpl extends ServiceImpl<CompanyWhitelistDroneTMapper, CompanyWhitelistDroneTDO> implements CompanyWhitelistDroneTService {

    @Autowired
    private CompanyWhitelistDroneTMapper companyWhitelistDroneTMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public R insertOrUpdate(WhiteDroneReqVo vo) {
        List<String> serialCodes = companyWhitelistDroneTMapper.selectAllSerialCode();
        if (serialCodes.contains(vo.getSerialCode())){
            throw new ServiceException("序列号已存在",405);
        }
        CompanyWhitelistDroneTDO companyWhitelistDroneTDO = new CompanyWhitelistDroneTDO();
        BeanUtils.copyProperties(vo, companyWhitelistDroneTDO);
        companyWhitelistDroneTDO.setStatus(1);

        companyWhitelistDroneTMapper.insert(companyWhitelistDroneTDO);
        return R.data("插入成功");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public R delete(Integer id) {
        companyWhitelistDroneTMapper.updateStatus(id);
        return R.data("删除成功");
    }

    @Override
    public R queryDroneByPage() {

        return R.data(companyWhitelistDroneTMapper.queryDroneByPage());
    }

}
